package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.demo.adapter.ChatAdapter;
import com.example.demo.entity.ChatContact;
import com.example.demo.entity.Message;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private TextView tvSend;
    private EditText edtMsg;
    private Context context;
    private ChatContact contact;
    private ChatAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        Intent intent = getIntent();
        contact = intent.getParcelableExtra(ContactActivity2.CONTACT);

        recyclerView = findViewById(R.id.recycler_view2);
        tvSend = findViewById(R.id.tv_send);
        edtMsg = findViewById(R.id.edt_msg);
        TextView tvTitle = findViewById(R.id.toolbar_title);
        tvTitle.setText(contact.getName());

        context = this;

        findViewById(R.id.toolbar_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);

       adapter = new ChatAdapter();
        recyclerView.setAdapter(adapter);



        edtMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(edtMsg.getText().toString()))
                {
                    tvSend.setEnabled(false);
                }
                else
                {
                    tvSend.setEnabled(true);
                }
            }
        });

        tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = edtMsg.getText().toString();
                Message message = new Message();
                message.setMsg(msg);
                adapter.setMessage(message);
                edtMsg.setText("");

                //滚动
                manager.scrollToPosition(adapter.getItemCount() - 1);

            }
        });
        initData();
    }

    private void initData()
    {
        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < 10; i++)
        {
            Message msg = new Message();
            msg.setType(i % 2 + 3);
            if (i % 2 == 0)
            {
                msg.setImg(R.drawable.a1);
            }
            else {
                msg.setImg(R.drawable.a2);
            }
            messages.add(msg);
        }
        adapter.setData(messages);
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
    }
}